home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dviselect / tfm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  2.2 KB  |  120 lines

  1. /*
  2.  * Copyright (c) 1987 University of Maryland Department of Computer Science.
  3.  * All rights reserved.  Permission to copy for any purpose is hereby granted
  4.  * so long as this copyright notice remains intact.
  5.  */
  6.  
  7. #ifndef lint
  8. static char rcsid[] = "$Header: tfm.c,v 1.1 88/02/11 17:08:59 jim Exp $";
  9. #endif
  10.  
  11. /*
  12.  * TFM file reading routines.
  13.  *
  14.  * TODO:
  15.  *    finish
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include "types.h"
  20. #include "fio.h"
  21. #include "tfm.h"
  22.  
  23. char    *malloc();
  24.  
  25. #define    ALLOC(n, type)    ((type *) malloc((unsigned) ((n) * sizeof (type))))
  26.  
  27. int
  28. readtfmfile(f, t, stopafterwidth)
  29.     register FILE *f;
  30.     register struct tfmdata *t;
  31.     int stopafterwidth;    /* ??? */
  32. {
  33.     i32 nc;
  34.  
  35.     if (trd_header(f, &t->t_hdr))
  36.         return (-1);
  37.     nc = t->t_hdr.th_ec - t->t_hdr.th_bc + 1;
  38.  
  39.     t->t_ci = NULL;
  40.     t->t_width = NULL;
  41.     t->t_height = NULL;
  42.     t->t_depth = NULL;
  43.  
  44.     (void) fseek(f, t->t_hdr.th_lh * 4L, 1);    /* XXX */
  45.  
  46.     if ((t->t_ci = ALLOC(nc, struct char_info_word)) == NULL ||
  47.         trd_ci(f, nc, t->t_ci) ||
  48.         (t->t_width = ALLOC(t->t_hdr.th_nw, i32)) == NULL ||
  49.         trd_fix(f, t->t_hdr.th_nw, t->t_width))
  50.         goto bad;
  51.     if (stopafterwidth)
  52.         return (0);
  53.     if ((t->t_height = ALLOC(t->t_hdr.th_nh, i32)) == NULL ||
  54.         trd_fix(f, t->t_hdr.th_nh, t->t_height) ||
  55.         (t->t_depth = ALLOC(t->t_hdr.th_nd, i32)) == NULL ||
  56.         trd_fix(f, t->t_hdr.th_nd, t->t_depth))
  57.         goto bad;
  58.     return (0);
  59.  
  60. bad:
  61.     if (t->t_ci != NULL)
  62.         free((char *) t->t_ci);
  63.     if (t->t_width != NULL)
  64.         free((char *) t->t_width);
  65.     if (t->t_height != NULL)
  66.         free((char *) t->t_height);
  67.     if (t->t_depth != NULL)
  68.         free((char *) t->t_depth);
  69.     return (-1);
  70. }
  71.  
  72. static int
  73. trd_header(f, th)
  74.     register FILE *f;
  75.     register struct tfmheader *th;
  76. {
  77.     register i32 *p;
  78.  
  79.     for (p = &th->th_lf; p <= &th->th_np; p++)
  80.         fGetWord(f, *p);
  81.     if (feof(f))
  82.         return (-1);
  83.     return (0);
  84. }
  85.  
  86. static int
  87. trd_ci(f, nc, ci)
  88.     register FILE *f;
  89.     register int nc;
  90.     register struct char_info_word *ci;
  91. {
  92.  
  93.     while (--nc >= 0) {
  94.         ci->ci_width = fgetbyte(f);
  95.         ci->ci_h_d = fgetbyte(f);
  96.         ci->ci_i_t = fgetbyte(f);
  97.         ci->ci_remainder = fgetbyte(f);
  98.         ci++;
  99.     }
  100.     if (feof(f))
  101.         return (-1);
  102.     return (0);
  103. }
  104.  
  105. static int
  106. trd_fix(f, nf, p)
  107.     register FILE *f;
  108.     register int nf;
  109.     register i32 *p;
  110. {
  111.  
  112.     while (--nf >= 0) {
  113.         fGetLong(f, *p);
  114.         p++;
  115.     }
  116.     if (feof(f))
  117.         return (-1);
  118.     return (0);
  119. }
  120.